package com.jisf.driver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jisf.driver.entity.dto.CoachListRequestDto;
import com.jisf.driver.entity.enumtype.SubjectEnum;
import com.jisf.driver.entity.po.Coach;
import com.jisf.driver.entity.po.DrivingSchoolLog;
import com.jisf.driver.entity.po.User;
import com.jisf.driver.entity.vo.*;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.mapper.CoachExamineMapper;
import com.jisf.driver.mapper.CoachMapper;
import com.jisf.driver.mapper.UserMapper;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.ICoachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisf.driver.utils.LogUtil;
import com.jisf.driver.utils.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-05-06
 */
@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements ICoachService {

    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private CoachExamineMapper coachExamineMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogUtil log;

    /**
     * 获取教练列表
     * @return
     */
    @Override
    public CoachListResponseVo getCoachList(CoachListVo coachListVo) {
        try {
            Page page = new Page(coachListVo.getPage(), coachListVo.getSize());
            CoachListRequestDto coachListRequestDto = new CoachListRequestDto();
            BeanUtils.copyProperties(coachListVo, coachListRequestDto);
            coachListRequestDto.setSubject(SubjectEnum.getEnum(coachListVo.getSubject()));
            List<CourseCoachVo> allCoach;
            if (coachListVo.getHaveAppoint()){
                allCoach = coachMapper.getHaveAppointCoach(coachListRequestDto, page);
            }else {
                allCoach = coachMapper.getAllCoach(coachListRequestDto, page);
            }
            long total = page.getTotal();
            CoachListResponseVo coachListResponseVo = new CoachListResponseVo(total, allCoach);

            return coachListResponseVo;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 获取所有教练
     * @return
     */
    @Override
    public List<ExamineCoachResponseVo> getAllCoach() {
        try {
            return coachMapper.getExamineAllCoach();
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 获取考试教练
     * @param id
     * @return
     */
    @Override
    public List<ExamineCoachResponseVo> getExamineCoach(Integer id) {
        try {
            return coachMapper.getExamineCoach(id);
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 教练获取个人信息
     * @return
     */
    @Override
    public CoachMessageVo getMessage() {
        try {
            Integer userid = UserUtil.userIdThreadLocal.get();
            CoachMessageVo message = coachMapper.getMessage(userid);
            return message;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 教练完善信息
     * @param coachMessageVo
     * @return
     */
    @Override
    public ResultCode completeMessage(CoachMessageVo coachMessageVo) {
        try {
            User user = new User();
            BeanUtils.copyProperties(coachMessageVo, user);
            int update = userMapper.updateById(user);

            // 存储日志
            String operator = UserUtil.usernameThreadLocal.get();
            String msg = String.format("用户名为%s的用户完善自己的信息", operator);

            if (update != 0){
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
                log.info(drivingSchoolLog);
                return ResultCode.MESSAGE_COMPLETE_SUCCESS;
            }else {
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
                log.info(drivingSchoolLog);
                return ResultCode.MESSAGE_COMPLETE_FAILED;
            }
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 获取所有教练信息
     * @return
     */
    @Override
    public CoachMessageResponseVo allCoach(UserListRequestVo userListRequestVo) {
        try {
            Page<User> page = new Page<>(userListRequestVo.getPage(), userListRequestVo.getSize());
            List<CoachMessageVo> message = coachMapper.allCoach(userListRequestVo, page);
            long total = page.getTotal();
            CoachMessageResponseVo coachMessageResponseVo = new CoachMessageResponseVo(message, total);
            return coachMessageResponseVo;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 修改教练信息
     * @param coachMessageVo
     * @return
     */
    @Override
    public ResultCode updateCoach(CoachMessageVo coachMessageVo) {
        try {
            User user = new User();
            BeanUtils.copyProperties(coachMessageVo, user);
            int updateById = userMapper.updateById(user);
            Coach coach = new Coach();
            BeanUtils.copyProperties(coachMessageVo, coach);
            UpdateWrapper<Coach> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userid", coachMessageVo.getId());
            int update = coachMapper.update(coach, updateWrapper);

            // 存储日志
            String operator = UserUtil.usernameThreadLocal.get();
            String msg = String.format("用户名为%s的用户修改了用户名为%s的用户的信息", operator, coachMessageVo.getUsername());

            if (update != 0 && updateById != 0) {
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
                log.info(drivingSchoolLog);
                return ResultCode.MESSAGE_UPDATE_COACH_SUCCESS;
            }
            String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
            log.info(drivingSchoolLog);
            return ResultCode.MESSAGE_UPDATE_COACH_FAILED;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }
}
